package com.py.firstproject.web;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.py.firstproject.pojo.Category;
import com.py.firstproject.pojo.Goods;
import com.py.firstproject.service.CategoryService;
import com.py.firstproject.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/listAll")
//实现一对多关联查询
    public List<Category> listAll(){
        //获取所有分类列表
        List<Category> list = categoryService.list(); //从数据库中获取所有category数据
        //遍历每个分类，查询关联的商品goods
        for(Category category : list){
            //用querywrapper构建查询条件，查询属于当前分类的商品，通过c_id关联
            QueryWrapper query = new QueryWrapper<Goods>();
            query.eq("c_id",category.getId());
            //查询符合条件的商品列表
            List<Goods> List2 = goodsService.list(query);
            //将查询到的商品列表存入category对象的goodlist属性中
            category.setGoodsList(List2);
        }
        return list;
    }






}
